package com.sdruixinggroup.mondayb2b.widget;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.ui.WebActivity;

/**
 * 通用图像
 */
public class ClassifyItemView extends FrameLayout
		implements View.OnClickListener {

	private ImageView mImgIv;

	private TextView mNameTv;

	private int mImgResId;

	private String mName;

	public ClassifyItemView(Context context) {
		super(context);
		initView();
	}

	public ClassifyItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initAttrs(attrs);
		initView();
		setOnClickListener(this);
	}

	private void initAttrs(AttributeSet attrs) {
		TypedArray a = getContext().obtainStyledAttributes(attrs,
				R.styleable.ClassifyItemView);
		mImgResId = a.getResourceId(R.styleable.ClassifyItemView_default_img,
				-1);
		mName = a.getString(R.styleable.ClassifyItemView_default_text);
		a.recycle();
	}

	private void initView() {
		LayoutInflater layoutInflater = LayoutInflater.from(getContext());
		View rootView = layoutInflater.inflate(R.layout.view_classify_item,
				this, true);
		mImgIv = (ImageView) rootView.findViewById(R.id.iv_classify_item);
		mNameTv = (TextView) rootView.findViewById(R.id.tv_classify_item);
		if (mImgResId != -1) {
			mImgIv.setImageResource(mImgResId);
		}
		mNameTv.setText(mName);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(getContext(), WebActivity.class);
		intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, "http://demo.chenc.site/#/linelist ");
		getContext().startActivity(intent);
	}
}
